package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.activity.Comment;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.CommentAdapter;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by bnhc on 17-6-4.
 */

public class CommentActivity extends Activity {
    private EditText activity_comment_et;
    private Button activity_comment_send_bt;
    private ListView commentActivity_lv;


    private Comment comment = new Comment();
    private String newsobjectId;
    private String userObjectId;
    private String userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commentlayout);
        newsobjectId = getIntent().getStringExtra("newsobjectId");
        userObjectId = (String) SharePreferencesUtil.getData(MyApplication.context, "userObjectId", "0000");
        userName = (String) SharePreferencesUtil.getData(MyApplication.context, "userName", "0000");
        initView();
        queryComment();
    }


    private void initView() {
        activity_comment_et = (EditText) findViewById(R.id.activity_comment_et);
        activity_comment_send_bt = (Button) findViewById(R.id.activity_comment_send_bt);
        activity_comment_send_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!activity_comment_et.getText().equals("")) {
                    comment.setContent(activity_comment_et.getText().toString());
                    //set UserObject  newsObject
                    comment.setNews_objectId(newsobjectId);
                    comment.setUser_objectId(userObjectId);
                    comment.setUser_name(userName);
                    comment.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(CommentActivity.this, "评论成功", Toast.LENGTH_LONG).show();
                                activity_comment_et.setText("");
                                queryComment();
                            } else {
                                Toast.makeText(CommentActivity.this, "评论失败", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                } else {
                    Toast.makeText(CommentActivity.this, "评论为空，请填写评论，再次尝试", Toast.LENGTH_LONG).show();
                }
            }
        });
        commentActivity_lv = (ListView) findViewById(R.id.commentActivity_lv);
    }

    private void queryComment() {
        BmobQuery<Comment> query_comment_list = new BmobQuery<>();
        query_comment_list.addWhereEqualTo("news_objectId", newsobjectId);
        query_comment_list.findObjects(new FindListener<Comment>() {
            @Override
            public void done(final List<Comment> list, BmobException e) {
                if (e == null) {
                    if (list.size() > 0) {
                        commentActivity_lv.setAdapter(new CommentAdapter(MyApplication.context, list, R.layout.commentactivity_listview_item));
                    } else {
                        Toast.makeText(CommentActivity.this, "暂无评论", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });


    }


}
